package com.GamersCom.games;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.GamersCom.db.GameRate;
import com.GamersCom.db.User;
import com.GamersCom.model.GameRateMgr;
import com.GamersCom.model.MgrFactory;
import com.GamersCom.model.UserMgr;

/**
 * Servlet implementation class RateGame
 */
public class RateGame extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String username = (String)session.getAttribute("username");
		String nextView;
		// only registered user can rate the game and only rate once.
		if ( username == null )
		{
			request.setAttribute("msg", "Login to rate the game!");
			nextView = "/login.jsp";
			request.getRequestDispatcher(nextView).forward(request, response);
		}
		else
		{
			// retrieve form data
			String rate = request.getParameter("rate");
			String gid = request.getParameter("id");
			// retrieve user id
			UserMgr userMgr = MgrFactory.getUserMgr();
			User user = userMgr.getUserByUsername(username);
			
			GameRateMgr gameRateMgr = MgrFactory.getGameRateMgr();
			GameRate gameRate = new GameRate();
			gameRate.setGameId(Integer.parseInt(gid));
			gameRate.setScore(Integer.parseInt(rate) );
			gameRate.setUserId(user.getId());
			
			//validate if user rates before			
			try
			{
				if ( gameRateMgr.getRateByUser(Integer.parseInt(gid), user.getId()) != null )					
				{
					request.setAttribute("msg", "FAIL");
					nextView = "/ajaxResult.jsp";
					request.getRequestDispatcher(nextView).forward(request, response);				
				} 
				else
				{
					//insert into db
					try
					{
						gameRateMgr.insert(gameRate);
						
						request.setAttribute("msg", "SUCCESS");	
						nextView = "/ajaxResult.jsp";						
						request.getRequestDispatcher(nextView).forward(request, response);						
					}
					catch (SQLException e)
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}	
					
				}
			}
			catch (NumberFormatException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();				
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			
			}
	
//			response.sendRedirect(nextView);	
//			request.getRequestDispatcher(nextView).forward(request, response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// get session
		HttpSession session = request.getSession();
		String username = (String)session.getAttribute("username");
		String nextView;
		// only registered user can rate the game and only rate once.
		if ( username == null )
		{
			request.setAttribute("msg", "Login to rate the game!");
			nextView = "/login.jsp";
			request.getRequestDispatcher(nextView).forward(request, response);
		}
		else
		{
			// retrieve form data
			String rate = request.getParameter("rate");
			String gid = request.getParameter("id");
			// retrieve user id
			UserMgr userMgr = MgrFactory.getUserMgr();
			User user = userMgr.getUserByUsername(username);
			
			GameRateMgr gameRateMgr = MgrFactory.getGameRateMgr();
			GameRate gameRate = new GameRate();
			gameRate.setGameId(Integer.parseInt(gid));
			gameRate.setScore(Integer.parseInt(rate) );
			gameRate.setUserId(user.getId());
			
			//validate if user rates before			
			try
			{
				if ( gameRateMgr.getRateByUser(Integer.parseInt(gid), user.getId()) != null )					
				{
//					request.setAttribute("msg", "You are only allowed to rate the game once!");
//					nextView = "/result.jsp";
//					request.getRequestDispatcher(nextView).forward(request, response);
					
					response.setContentType("text/xml");
					response.setHeader("Cache-Control", "no-cache");
					response.getWriter().write("<message>FAIL</message>");
					
				} 
				else
				{
					//insert into db
					try
					{
						gameRateMgr.insert(gameRate);
						
						response.setContentType("text/xml");
						response.setHeader("Cache-Control", "no-cache");
						response.getWriter().write("<message>SUCCESS</message>");
						
//						request.setAttribute("msg", "Thanks for your rate!");	
//						nextView = "/result.jsp";						
//						request.getRequestDispatcher(nextView).forward(request, response);
						
					}
					catch (SQLException e)
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}	
					
				}
			}
			catch (NumberFormatException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
				
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			
			}
	
//			response.sendRedirect(nextView);	
//			request.getRequestDispatcher(nextView).forward(request, response);
		}
		
	}

}
